//
//  ViewController.m
//  TransfromTest
//
//  Created by wangrui on 13-10-14.
//  Copyright (c) 2013年 PPS. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    UIView *testView;
}
@end

@implementation ViewController

- (void)loadView{
    [super loadView];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(rotate:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view showBounds];
    
	// Do any additional setup after loading the view, typically from a nib.
    testView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 50, 100)];
    testView.backgroundColor = [UIColor lightGrayColor];
    [testView showBounds];
    [self.view addSubview:testView];
    [self.view sendSubviewToBack:testView];
    
}


- (void)rotate:(NSNotification*)noti{
    /**
     * 自己来模拟RootView controller的View 大小为什么不变
     */
    
    NSLog(@"before transform %@",NSStringFromCGRect(self.view.frame));
    
    //可以肯定的window改变了rootView的transform. 所以第一步先将其还原
    self.view.transform = CGAffineTransformIdentity;
    
    
    /**
     * 以下为代码段(1), 注释与打开注释观察效果，注释掉之后，view不会跟着旋转了,因为设置了CGAffineTransformIdentity
     */
    UIInterfaceOrientation newOrientation = [[noti.userInfo valueForKey:UIApplicationStatusBarOrientationUserInfoKey] integerValue];
    switch (newOrientation) {
        case UIInterfaceOrientationLandscapeRight:
            self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
            break;
            
        case UIInterfaceOrientationLandscapeLeft:
            self.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
            break;
        case UIInterfaceOrientationPortrait:
            self.view.transform = CGAffineTransformIdentity;
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            self.view.transform = CGAffineTransformMakeRotation(M_PI);
            break;
            
        default:
            break;
    }

    
    //此时transform虽然改了，但是坐标并没有立即更改. 真正旋转完成了时,window 会根据设置的transform改变了坐标
    //self.view.width += 15; //这个可以验证改变frame在当前函数有用,但完成时恢复了状态
    NSLog(@"after transform %@",NSStringFromCGRect(self.view.frame));
}



/*
 *window 会在剪裁视图后调用这个方法。 这和通知中心调设备更改发出的通知不同。 那个可能会先调用
 */
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    NSLog(@"did rotate %@",NSStringFromCGRect(self.view.frame));
}


- (BOOL)shouldAutorotate{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAll;
}


- (IBAction)btclick:(id)sender{

    
    /**
     * 设置view的tranfrom,会改变view的frame,其坐标系是相对于父视图的坐标系为参考
     * UIScreen、UIWindow的坐标原点始终在设备的左上点(靠近摄像头的那个左角），设备旋转时更换到左下点或右下点, 设备短的那一边始终为X
     * UIView 如果不是rootview的话，坐标原点在父视图的左上角，不管设备方向
     * 如果是rootview,其坐标点在window相应的坐标点（因为window作了相应的处理)
     */
    
    
    /*
     *以下两行代码可以证明rootview相对window的起始点为设备的左上点(不是view的左上点),旋转不同的方向,看view是怎样移动的!
     */
    //self.view.centerX += 5;
    //self.view.centerY += 5;
    
    
    
//    testView.transform = CGAffineTransformMakeRotation(M_PI/3);
//    testView.transform = CGAffineTransformMakeScale(1.2, 1.2);
//    testView.transform = CGAffineTransformIdentity;
//    testView.frame = CGRectMake(0, 0, 100, 100);
//    

    
//    static int i = 0;
//    i++;
//    testView.transform = CGAffineTransformMakeRotation((i%4)*M_PI_2);
//    testView.left = 0.0;
//    
//    NSLog(@"IsIdentity:%d,%@",CGAffineTransformIsIdentity(testView.transform),NSStringFromCGAffineTransform(testView.transform));
//    NSLog(@"testView frame %@",NSStringFromCGRect(testView.frame));
    
    NSLog(@"%@",self.view);

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
